var btns = $(".pages span");
var bookBox = $(".box-con");
var test = $(".pages .active");
var num = 0;
//var bookItem = $(".box-con li button");
//----------点击页数----------
btns.click(function(){
	var page = $(this).text();
	test.attr("class","");
	$(this).addClass("active");
	test = $(this);
	$.ajax({
		type:"get",
		url:"pageHandle.php?action=page",
		data:{
			page:page
		},
		success:function(res){
			var obj = $.parseJSON(res),
			 	result = obj.result,
				len = result.length;
			if(obj.errNo==0){
				var str = "";
				for(let i = 0;i < len;i++){
					str += "<li data-id = "+result[i].id+">"+
					"<a href='bookDetil.php?id="+result[i].id +"'>"+
							"<img src="+result[i].src+" alt='' />"+
							"<p class='book-name'>"+result[i].bookname+"</p>"+
							"<p class='auther'>"+result[i].author+"</p>"+
							"<p class='price'>￥<span>"+result[i].price+"</span></p>"+	
						"</a>"+
						"<button>加入购物车</button>"+
					"</li>"
				}
				bookBox.html(str);
			}
		}
	})
})
var maxPage = $(".befor").prev().html();
var page = test.text();
//-----------点击上一页下一页----------
$(".pages .btn").click(function(){
	if($(this).hasClass("after")){
		page--;
		page = page < 1? maxPage:page;	
	}else{
		page++;
		page = page > maxPage? 1:page;
	}
	$.ajax({
		type:"get",
		url:"pageHandle.php?action=page",
		data:{
			page:page
		},
		success:function(res){
			var obj = $.parseJSON(res),
			 	result = obj.result,
				len = result.length;
			if(obj.errNo==0){
				var str = "";
				for(let i = 0;i < len;i++){
					str += "<li data-id = "+result[i].id+">"+
					"<a href='bookDetil.php?id="+result[i].id+"'>"+
							"<img src="+result[i].src+" alt='' />"+
							"<p class='book-name'>"+result[i].bookname+"</p>"+
							"<p class='auther'>"+result[i].author+"</p>"+
							"<p class='price'>￥<span>"+result[i].price+"</span></p>"+	
						"</a>"+
						"<button>加入购物车</button>"+
					"</li>"
				}
				bookBox.html(str);
			}
		}
	})
	test.attr("class","");
	btns.eq(page-1).addClass("active");
	test = btns.eq(page-1);
})
//---------在图书列表页点击加入购物车-----------
$(".box-con").click(function(e){
	var target = $(e.target);
	if(target.text()=="加入购物车"){
		var username ;
		num++;
		if($(".header li>span>a")[0]){
			target.parent().append("<p>请先登录下账号</p>");
			$(" .con-right").show();
			return;
		}else{
			var str = $(".header li>span").eq(1).text().replace(/\s/g,"");
			username = str.split(",")[0];
		}
		var id = target.parent().attr("data-id");
		$(".foodsCar-num")[0].innerHTML++;
		$.ajax({
			type:"post",
			url:"pageHandle.php?action=putShopcar",
			async:true,
			data:{
				id:id,
				username:username,
				bookNum:num
			},
			success:function(res){
				console.log(res);
			}
		})
	}
})